package com.ling.levelCon
{
	import com.ling.events.CreateLevelEvent;
	import com.ling.ui.CreateNewLevelUI;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.managers.PopUpManager;
	
	import spark.components.Image;

	/**
	 * main logic of levelList UI
	 * @author hongxuan
	 * 
	 */	
	public class LevelListControl extends EventDispatcher
	{
		private var addBtn:Image;
		private var delBtn:Image;
		private var _app:LingEditor;
		private var createNewUI:CreateNewLevelUI;
		
		private var _currentLvel:uint;
		
		public function LevelListControl(app:LingEditor)
		{
			_app = app;
			
			addBtn = app.newLevelBtn;
			delBtn = app.delLevelBtn;
			
			addBtn.addEventListener(MouseEvent.CLICK,addNewLevel);
			delBtn.addEventListener(MouseEvent.CLICK,deleteLevel);
			//
			createNewUI = new CreateNewLevelUI();
			createNewUI.addEventListener(Event.COMPLETE,onCreateComp);
		}
		
		
		/**
		 * create a new level
		 * @param event
		 * 
		 */		
		private function addNewLevel(event:MouseEvent):void
		{
			PopUpManager.addPopUp(createNewUI,_app,true);
			PopUpManager.centerPopUp(createNewUI);
		}
		/**
		 * delete current select level
		 * @param event
		 * 
		 */		
		private function deleteLevel(event:MouseEvent):void
		{
			
		}
		
		
		private function onCreateComp(event:Event):void
		{
			//trace(createNewUI.level);
			_currentLvel = createNewUI.level;
			dispatchEvent(new CreateLevelEvent(CreateLevelEvent.CREATE_NEW_LEVEL,_currentLvel,createNewUI.xNum,createNewUI.yNum));
		}
	}
}